#!/bin/bash

if [ -n "$1" ] ; then
  data_date=$1
else
  data_date=`date -d "-1 days" +%F`
fi

DWS_EVENT_LOG_SESSION_AGG_SQL="
INSERT OVERWRITE TABLE jtp_app_dws.dws_event_log_session_agg PARTITION (dt='${data_date}')
SELECT
    device_id, device_type, session_id, province, city
    , count(session_id) AS session_pv
    , max(event_ts) - min(event_ts) AS session_during_time
    , split(
        sort_array(
                collect_list(
                        if(
                            event_id='pageView', concat(event_ts, '_', properties['url']), null
                        )
                )
        )[0], '_'
    )[1] AS session_entry_url
FROM jtp_app_dwd.dwd_event_log_detail
WHERE dt='${data_date}'
GROUP BY device_id, device_type, session_id, province, city
;
"

/opt/module/spark/bin/beeline -u jdbc:hive2://192.168.88.101:10001 -n bwie -e "${DWS_EVENT_LOG_SESSION_AGG_SQL}"